# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: kuwo_老师代码.py
@time: 2021/8/21 13:10
'''
# encoding: utf-8
import icecream

"""
@author: 夏洛
@QQ: 1972386194
@file: demo1.py
"""
import os
import re

import requests

'''
歌曲采集
-- 分析
    找入口 模拟用户行为
        浏览器抓包
        抓包工具监听

    数据地址：http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=%E8%8F%B2%E8%8F%B2&pn=1&rn=30&httpsStatus=1&reqId=53abc9f1-00e9-11ec-b70d-a1b2f46f8474

    找歌曲地址：http://www.kuwo.cn/url?format=mp3&rid=172403633&response=url&type=convert_url3&br=128kmp3&from=web&t=1629376428666&httpsStatus=1&reqId=b3dda4b0-00e9-11ec-b2d0-c9dc4efda2c5
     真实的歌曲地址
    "url": "https://gu-sycdn.kuwo.cn/03735f995ef9a7efe775ca90ab2dcd8d/611e4fc2/resource/n3/68/58/678270135.mp3"

    总结
        找参数  
        写程序  
            发请求
                注意参数
            提数据
                根据需要的参数提取
            保存数据
                注意文件名字

'''
from utils import base
import json
import urllib.parse
from loguru import logger


class Crawl(base.Spider):

    def __init__(self, key):
        self.headers = {}
        self.key = urllib.parse.quote(key)
        print(self.key)

    def validateTitle(self, title):
        # 去除文件不识别符号
        punctuation = '!,;:?"\'、，；“ ” 《 》【】？ + * & / |'
        new_title = re.sub(r'[{}]+'.format(punctuation), '', title)
        return new_title.strip()

    def crawl(self):
        cookies = {
            'Hm_lvt_cdb524f42f0ce19b169a8071123a4797': '1629510199',
            '_ga': 'GA1.2.2085152892.1629510199',
            '_gid': 'GA1.2.1035340629.1629510199',
            'Hm_lpvt_cdb524f42f0ce19b169a8071123a4797': '1629516973',
            'kw_token': 'VDEGL3YI0SJ',
        }
        headers = {
            'csrf': 'VDEGL3YI0SJ',  # 不变的  不会过期的
            'Referer': 'http://www.kuwo.cn/search/list?key=' + self.key,
            'Connection': 'keep-alive',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache',
            'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
            'Accept': 'application/json, text/plain, */*',
            'sec-ch-ua-mobile': '?0',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Accept-Language': 'zh-CN,zh;q=0.9',
             }
        params = (
            ('key', '\u674E\u767D'),
            ('httpsStatus', '1'),
            ('reqId', 'ce987a90-023e-11ec-990d-c186eca62f87'),
        )

        # https://kuwo.cn/api/www/search/searchMusicBykeyWord?%E5%91%A8%E6%9D%B0%E4%BC%A6&pn=1&rn=30&httpsStatus=1&reqId=cee1dfa0-023e-11ec-990d-c186eca62f87
        url = f'https://kuwo.cn/api/www/search/searchMusicBykeyWord?key={self.key}&pn=1&rn=30&httpsStatus=1&reqId=cee1dfa0-023e-11ec-990d-c186eca62f87'
        icecream.ic(url)
        res = requests.get(url, headers = headers,cookies = cookies,params = params)
        print(res.json())
        music_data = res.json().get('data')['list']
        for i in music_data:
            # 提取歌曲信息  rid  name
            title = i.get('name')
            titles = self.validateTitle(title)
            rid = i.get('rid')
            #https://rl01-sycdn.kuwo.cn/4b4e35bb5ca1591198951e2bdf2b4f47/61209efd/resource/n1/81/92/2570640440.mp3
            music_url = f'http://www.kuwo.cn/url?format=mp3&rid={rid}&response=url&type=convert_url3&br=128kmp3&from=web&t=1629376428666&httpsStatus=1&reqId=b3dda4b0-00e9-11ec-b2d0-c9dc4efda2c5'
            # http://www.kuwo.cn/url?format=mp3&rid=172403633&response=url&type=convert_url3&br=128kmp3&from=web&t=1629377126449&httpsStatus=1&reqId=53c6e121-00eb-11ec-ad11-3f4cfc483edb
            play = requests.get(music_url, headers = headers,cookies = cookies,params = params)
            html = json.loads(play.content.decode())
            music = html.get('url')  # 歌曲地址
            # 文件夹命名  正常用 搜索入口参数
            save_path = os.path.join(os.getcwd() + '/music')
            if os.path.exists(save_path) is False:
                os.mkdir(save_path)
            with open(save_path + '/' + titles + '.mp3', 'wb') as f:
                # f.write(self.fetch(music, headers = headers).content)
                logger.info(f'真在提取{title}')


s = Crawl('李白')
s.crawl()













